<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="reportName">
        <el-input
          v-model="queryParams.reportName"
          placeholder="标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="编号" prop="reportNo">
        <el-input
          v-model="queryParams.reportNo"
          placeholder="编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
	  <el-form-item label="起始日期" prop="startDate">
	    <el-date-picker clearable
	      v-model="startDate"
	      type="date"
	      value-format="yyyy-MM-dd"
	      placeholder="请选择起始日期">
	    </el-date-picker>
	  </el-form-item>
	  <el-form-item label="截止日期" prop="endDate">
	    <el-date-picker clearable
	      v-model="endDate"
	      type="date"
	      value-format="yyyy-MM-dd"
	      placeholder="请选择截止日期">
	    </el-date-picker>
	  </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:report:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="reportList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" align="center" prop="reportId" />
	  <el-table-column label="报表编号" align="center" prop="reportNo" />
      <el-table-column label="报表标题" align="center" prop="reportName" />
      <el-table-column label="咨询方式" align="center" prop="consultType">
		  <template slot-scope="scope">
			<span>{{ scope.row.consultType== 1 ? '个体咨询': '团体咨询'  }}</span>
		  </template>
      </el-table-column> 
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:report:edit']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:report:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="报表标题" prop="reportName">
          <el-input v-model="form.reportName" placeholder="请输入报表标题" />
        </el-form-item>
        <el-form-item label="咨询类型" prop="consultType">
		  <el-select v-model="form.consultType" name="consultType" >
			<el-option label="个体咨询" value="1"></el-option>
			<el-option label="团体(咨询、讲座、辅导、工作坊、其他)" value="2"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listReport, getReport, delReport, addReport } from "@/api/consult/report";

export default {
  name: "Report",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      reportList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        reportName: null,
        consultType: null,
        reportNo: null,
		params: {
			
		}
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        reportName: [
          { required: true, message: "$comment不能为空", trigger: "blur" }
        ],
        consultType: [
          { required: true, message: "1个人 2 团体不能为空", trigger: "change" }
        ]
      },
	  startDate: '',
	  endDate: ''
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listReport(this.queryParams).then(response => {
        this.reportList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        reportId: null,
        reportName: null,
        consultType: null,
        reportNo: null,
        createBy: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.reportId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新建报表";
    },
    /** 查看按钮操作 */
    handleUpdate(row) {
      const reportId = row.reportId;
	  const consultType = row.consultType;
      this.$router.push("/consult/report/group/"+consultType+"/" + reportId);
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
			const params = {
				title: this.form.reportName
			}
		  this.$router.push({
			  path: "/consult/report/add/"+this.form.consultType,
			  query: params
		  });
		  this.open = false;
          /*if (this.form.reportId != null) {
            updateReport(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addReport(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          } */
		  
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const reportIds = row.reportId || this.ids;
      this.$modal.confirm('是否确认删除报表序号为"' + reportIds + '"的数据项？').then(function() {
        return delReport(reportIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/report/export', {
        ...this.queryParams
      }, `report_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
